package com.woosiyuan.atie.demo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;

import com.woosiyuan.atierobot.ArmStateListener;
import com.woosiyuan.atierobot.InfraredListener;
import com.woosiyuan.atierobot.LightStateListener;
import com.woosiyuan.atierobot.MyPowerManager;
import com.woosiyuan.atierobot.utils.RobotBatteryUtils;

public class MainActionReceiver extends BroadcastReceiver {
	private final static String TAG = "MainActionReceiver";
	public final static  String ACTION_BATTERY_CHANGED = "com.woosiyuan.atie.ACTION_BATTERY_CHANGED";
	public final static  String ACTION_POWER_CONNECTED = "com.woosiyuan.atie.ACTION_POWER_CONNECTED";
	public final static  String ACTION_POWER_DISCONNECTED = "com.woosiyuan.atie.ACTION_POWER_DISCONNECTED";
	public final static  String ACTION_POWER_CHARGING = "com.woosiyuan.atie.ACTION_POWER_CHARGING";

	public final static String ACTION_INFRARED_FORWARD  = "com.woosiyuan.atie.ACTION_INFRARED_FORWARD";
	public final static String ACTION_INFRARED_BACKWARD = "com.woosiyuan.atie.ACTION_INFRARED_BACKWARD";
	public final static String ACTION_INFRARED_LEFT     = "com.woosiyuan.atie.ACTION_INFRARED_LEFT";
	public final static String ACTION_INFRARED_RIGHT    = "com.woosiyuan.atie.ACTION_INFRARED_RIGHT";
	public final static String ACTION_WAKEUP_KEY        = "com.woosiyuan.atie.ACTION_WAKEUP_KEY";


	public final static String ACTION_LIGHT    = "com.woosiyuan.atie.ACTION_LIGHT";
	public final static String ACTION_HEAD_ARM = "com.woosiyuan.atie.ACTION_HEAD_ARM";

	public static final String ACTION_GLOBAL_BUTTON = "android.intent.action.GLOBAL_BUTTON";

	private InfraredListener mInfraredListener;
	private ArmStateListener mArmStateListener;

	private LightStateListener mLightStateListener;

	@Override
	public void onReceive(Context context, Intent intent) {
		this.mInfraredListener = MainActivity.mInfraredListener;
		this.mArmStateListener = MainActivity.mArmStateListener;
		String action = intent.getAction();
		Log.i(TAG, "ATIE  Launcher boardcast: " + action);
		switch (action) {
			case ACTION_BATTERY_CHANGED:
				//电池电量改变，插上电源，拔掉电源广播
				//电池电量变化
				//你好
				RobotBatteryUtils.getInstance().onReceive(intent);
				break;
			case ACTION_POWER_CONNECTED:
				//Log.i(TAG,"Broadcast ACTION_POWER_CONNECTED");
				RobotBatteryUtils.getInstance().setIsCharging(true); //充电状态变化

				RobotBatteryUtils.getInstance().onReceive(intent);//电池电量变化
				break;
			case ACTION_POWER_DISCONNECTED:
				//Log.i(TAG,"Broadcast ACTION_POWER_DISCONNECTED");
				RobotBatteryUtils.getInstance().setIsCharging(false); //充电状态变化

				RobotBatteryUtils.getInstance().onReceive(intent);//电池电量变化
				break;
			case ACTION_POWER_CHARGING:
				//Log.i(TAG,"Broadcast ACTION_POWER_CHARGING");
				RobotBatteryUtils.getInstance().onChargeError(intent);//充电错误
				break;
			case ACTION_WAKEUP_KEY:
				//LogUtil.i("Broadcast ACTION_WAKEUP_KEY 唤醒按钮");
				MyPowerManager.wakeUpAndUnlock();
				break;
			case ACTION_INFRARED_FORWARD:
				if (mInfraredListener != null)
					mInfraredListener.getMsg(1);
				break;
			case ACTION_INFRARED_BACKWARD:
				if (mInfraredListener != null)
					mInfraredListener.getMsg(2);
				break;
			case ACTION_INFRARED_LEFT:
				if (mInfraredListener != null)
					mInfraredListener.getMsg(3);
				break;
			case ACTION_INFRARED_RIGHT:
				if (mInfraredListener != null)
					mInfraredListener.getMsg(4);
				break;
			case ACTION_LIGHT:
				if (mArmStateListener != null)
					mArmStateListener.getState(intent.getBooleanExtra("isOpen", false));
				break;
			case ACTION_HEAD_ARM:
				if (mArmStateListener != null)
					mArmStateListener.getState(intent.getBooleanExtra("isUp", false));
				break;
			case ACTION_GLOBAL_BUTTON:
				KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
				if (event.getKeyCode() == KeyEvent.KEYCODE_NOTIFICATION &&
						event.getAction() == KeyEvent.ACTION_UP) {
					MainActivity.wakeUp();//唤醒方法位置按项目需求自行添加调用
					Log.e(TAG, "onReceive: "+"接收到了唤醒的广播xxxx" );
				}
		}
	}
}



